{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.8-final"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python3",
   "display_name": "Python 3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "['sss sbb'] ('sss sbb',)\n"
    }
   ],
   "source": [
    "'''\n",
    "Question 4\n",
    "Question:\n",
    "Write a program which accepts a sequence of comma-separated numbers from console and generate a list and a tuple which contains every number.Suppose the following input is supplied to the program:\n",
    "\n",
    "34,67,55,33,12,98\n",
    "\n",
    "Then, the output should be:\n",
    "\n",
    "['34', '67', '55', '33', '12', '98'] ('34', '67', '55', '33', '12', '98')\n",
    "'''\n",
    "\n",
    "con = input().split(',')\n",
    "print(list(con),tuple(con))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "123\n"
    }
   ],
   "source": [
    "'''\n",
    "Question 5:\n",
    "Define a class which has at least two methods:\n",
    "\n",
    "getString: to get a string from console input\n",
    "printString: to print the string in upper case.\n",
    "Also please include simple test function to test the class methods.\n",
    "'''\n",
    "\n",
    "class Foo:\n",
    "    def __init__(self):\n",
    "        self.str = str()\n",
    "\n",
    "    def getStr(self):\n",
    "        self.str = input()\n",
    "\n",
    "    def printStr(self):\n",
    "        print(self.str.upper())\n",
    "\n",
    "foo = Foo()\n",
    "foo.getStr()\n",
    "foo.printStr()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "22,26,32\n"
    }
   ],
   "source": [
    "'''\n",
    "Question 6:\n",
    "Write a program that calculates and prints the value according to the given formula:\n",
    "\n",
    "Q = Square root of [(2 C D)/H]\n",
    "\n",
    "Following are the fixed values of C and H:\n",
    "\n",
    "C is 50. H is 30.\n",
    "\n",
    "D is the variable whose values should be input to your program in a comma-separated sequence.For example Let us assume the following comma separated input sequence is given to the program:\n",
    "\n",
    "100,150,180\n",
    "\n",
    "The output of the program should be:\n",
    "\n",
    "18,22,24\n",
    "'''\n",
    "from math import sqrt # 　计算开方的函数，来自math库\n",
    "\n",
    "def calc(d):\n",
    "    c = 50\n",
    "    h = 30\n",
    "    return sqrt((2*c*d)/h)\n",
    "\n",
    "q = input()\n",
    "lst_q = q.split(',')\n",
    "d = [str(round(calc(int(i)))) for i in lst_q]\n",
    "print(','.join(d))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "18,22,32\n"
    }
   ],
   "source": [
    "# method 2\n",
    "def calc_map(d):\n",
    "    c = 50\n",
    "    h = 30\n",
    "    return str(round(sqrt((2*c*int(d))/h)))\n",
    "\n",
    "q = input().split(',')\n",
    "d = list(map(calc_map, q))\n",
    "print(','.join(d))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]\n"
    }
   ],
   "source": [
    "'''\n",
    "Question 7\n",
    "Question:\n",
    "Write a program which takes 2 digits, X,Y as input and generates a 2-dimensional array. The element value in the i-th row and j-th column of the array should be i j.*\n",
    "Note: i=0,1.., X-1; j=0,1,¡­Y-1. Suppose the following inputs are given to the program: 3,5\n",
    "Then, the output of the program should be:\n",
    "[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]\n",
    "'''\n",
    "x, y = map(int,input().split(','))\n",
    "lst = []\n",
    "for i in range(x):\n",
    "    tem = []\n",
    "    for j in range(y):\n",
    "        tem.append(i * j)\n",
    "    lst.append(tem)\n",
    "\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "[[0, 0, 0, 0, 0], [0, 1, 2, 3, 4], [0, 2, 4, 6, 8]]\n"
    }
   ],
   "source": [
    "#Method 2\n",
    "x, y = map(int,input().split(','))\n",
    "lst = [[i * j for i in range(y)] for j in range(x)]\n",
    "print(lst)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "['ere', 'ggg', 'sdf', 'sfsf']\n"
    }
   ],
   "source": [
    "'''\n",
    "Question 8\n",
    "Question:\n",
    "Write a program that accepts a comma separated sequence of words as input and prints the words in a comma-separated sequence after sorting them alphabetically.\n",
    "\n",
    "Suppose the following input is supplied to the program:\n",
    "\n",
    "without,hello,bag,world\n",
    "\n",
    "Then, the output should be:\n",
    "\n",
    "bag,hello,without,world\n",
    "'''\n",
    "a = input().split(',')\n",
    "a.sort()\n",
    "print(a)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "HELLO\nPRACTISE\n"
    }
   ],
   "source": [
    "'''\n",
    "Question 9\n",
    "Question:\n",
    "Write a program that accepts sequence of lines as input and prints the lines after making all characters in the sentence capitalized.\n",
    "\n",
    "Suppose the following input is supplied to the program:\n",
    "\n",
    "Hello world\n",
    "Practice makes perfect\n",
    "\n",
    "Then, the output should be:\n",
    "\n",
    "HELLO WORLD\n",
    "PRACTICE MAKES PERFECT\n",
    "'''\n",
    "#Method 1\n",
    "lst = []\n",
    "\n",
    "while True:\n",
    "    x = input()\n",
    "    if len(x) == 0:\n",
    "        break\n",
    "    lst.append(x.upper())\n",
    "\n",
    "for line in lst:\n",
    "    print(line)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "tags": []
   },
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": "HELLO\nWORLD\n"
    }
   ],
   "source": [
    "'''\n",
    "两种方法的结果不一致，本方法是输入一行就打印一行，输入空格则退出。\n",
    "'''\n",
    "\n",
    "#Method 2\n",
    "def get_input():\n",
    "    while True:\n",
    "        s = input()\n",
    "        if not s:\n",
    "            return\n",
    "        yield s\n",
    "\n",
    "for line in map(str.upper,get_input()):\n",
    "    print(line)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ]
}